/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.data.template.section;

import jigdo.JigdoException;
import org.itadaki.bzip2.BZip2InputStream;
import jigdo.data.template.JigdoTemplate;

/**
 *
 * @author John R Sohn
 */
public class TemplateBZIPSection extends TemplateSection {
  

    public TemplateBZIPSection(JigdoTemplate parent)
    {
        super(parent);
        this.SectionType = jigdo.data.template.TemplateType.BZIP;
    }
    
   public int UncompressedLength;
    public byte[] UncompressedData;

   @Override
   public void Interpret()throws jigdo.JigdoException {
        int[] uncomplength = new int[6];
        System.arraycopy(DataChunk, 0, uncomplength, 0, 6);

        this.UncompressedLength =
             (int)   jigdo.utils.ByteUtils.GetIntFromArray(uncomplength,6);

        byte[] tempcomp = new byte[this.Length - 6];

        System.arraycopy(DataChunk, 7, tempcomp, 0, tempcomp.length);

        this.UncompressedData = new byte[this.UncompressedLength];

        //UncompressedData = new ArrayList<Byte>();
        try {

    org.itadaki.bzip2.BZip2InputStream bzipper=
            new org.itadaki.bzip2.BZip2InputStream(new java.io.ByteArrayInputStream(tempcomp),true);


    
            bzipper.read(this.UncompressedData, 0, this.UncompressedLength);

            bzipper.close();

        } catch (java.io.IOException e) {
            throw new JigdoException("Error encountered when decompressing BZIP section", e);
        }
        
        if (!TemplateSection.RetainDataChunk)
        {
            this.DataChunk = null;
        }
   }
}
